//
//  progress.m
//  EFExtend
//
//  Created by mac on 15/3/25.
//  Copyright (c) 2015年 Eric. All rights reserved.
//

#import "progress.h"


@interface progress ()
@property (nonatomic,strong) HMUIWebView *web;
@property (nonatomic,strong) HMUIProgressView *progress;
@end

@implementation progress
@synthesize web;
@synthesize url;
@synthesize progress=_progress;
- (void)dealloc
{
    self.web = nil;
    self.progress = nil;
    self.url = nil;
    HM_SUPER_DEALLOC();
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self.customNavLeftBtn setImage:[UIImage imageNamed:@"back.png"] forState:UIControlStateNormal];
    [self.customNavLeftBtn setFrame:CGRectMakeBound(32, 32)];
    
    HMUIProgressView *progress = [[HMUIProgressView alloc]init];
    progress.progressType = UIProgressTypeLine;
    [self.view addSubview:progress];
    [progress mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.view.mas_top).offset(0);
        make.left.mas_equalTo(self.view.mas_left).offset(0);
        make.width.mas_equalTo(200);
        make.height.mas_equalTo(40);
    }];
    progress.roundedCorners = NO;
    progress.radius = 0.f;
    progress.trackTintColor = [UIColor md_amber_100];
    progress.trackBorderTintColor = [UIColor md_red_100];
    progress.progressTintColor = [UIColor md_blue_200];
    progress.progress=.5;
    
    [self delayInMain:1.f invoke:^{
        [progress setProgress:.3f animated:YES];
        [self delayInMain:1.f invoke:^{
            [progress setProgress:.6f animated:YES];
            [self delayInMain:1.f invoke:^{
                [progress setProgress:1.f animated:YES];
            }];
        }];
    }];
    
    progress = [[HMUIProgressView alloc]init];
    progress.progressType = UIProgressTypeCircle;
    [self.view addSubview:progress];
    [progress mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.view.mas_left).offset(0);
        make.top.mas_equalTo(self.view.mas_top).offset(50);
        make.width.mas_equalTo(200);
        make.height.mas_equalTo(200);
    }];
    progress.roundedCorners = NO;
    progress.radius = 0.f;
    progress.trackTintColor = [UIColor md_amber_100];
    progress.trackBorderTintColor = [UIColor md_red_100];
    progress.progressTintColor = [UIColor md_blue_200];
    progress.progress=.5;
    
    [self delayInMain:1.f invoke:^{
        [progress setProgress:.3f animated:YES];
        [self delayInMain:1.f invoke:^{
            [progress setProgress:.6f animated:YES];
            [self delayInMain:1.f invoke:^{
                [progress setProgress:1.f animated:YES];
            }];
        }];
    }];
    
    
    HMUIStarView * _star = [[HMUIStarView alloc]init];
    _star.tintColor = [UIColor clearColor];
    _star.maximumValue = 5;
    _star.minimumValue = 0;
    _star.allowsHalfStars = YES;
    _star.spacing = 1;
    [_star EFOwner:self.view];
    _star.backgroundColor = RGB(1., 0, 0);
    [_star mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(progress.mas_bottom).offset(5);
//        make.height.mas_equalTo(40);
//        make.width.mas_equalTo(200);
        make.left.mas_equalTo(progress.mas_left).offset(10);
    }];
    
    _star.value=4;
    _star.emptyStarImage = [UIImage imageNamed:@"star_0"];//[[UIImage imageNamed:@"star_0"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
    _star.filledStarImage = [UIImage imageNamed:@"star_1"];//[[UIImage imageNamed:@"star_1"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
    _star.halfStarImage = [UIImage imageNamed:@"star_2"];//[[UIImage imageNamed:@"star_2"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
    
    [[[HMHTTPRequestOperationManager sharedInstance] flower] clear];
    NSArray * cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies];
    INFO(cookies);
    //    if (self.url) {
    //        self.web = [HMUIWebView spawn];
    //        self.web.url = @"http://pan.baidu.com/s/1jGmWUmq";//@"http://pan.baidu.com/s/1mgFZhrU";//@"http://cloud.letv.com/s/wN6jusKG";//
    //        [self.view addSubview:self.web];
    //        [self.web mas_makeConstraints:^(MASConstraintMaker *make) {
    //            make.top.mas_equalTo(self.view.mas_top).offset(0);
    //            make.left.mas_equalTo(self.view.mas_left).offset(0);
    //            make.bottom.mas_equalTo(self.view.mas_bottom).offset(0);
    //            make.right.mas_equalTo(self.view.mas_right).offset(0);
    //        }];
    //    }
}

ON_WebView(signal){
    if ([signal is:[HMUIWebView WILL_START]]) {
        INFO(self.web.request.allHTTPHeaderFields);
        INFO(self.web.loadingURL);
        
        if ([self.web.loadingURL rangeOfString:@"baiduyun://"].location!=NSNotFound) {
            [self open:self.web.loadingURL animate:YES];
            [signal returnNO];
            return;
        }
        
        if ([self.web.loadingURL rangeOfString:@"http://117.27.241.33"].location!=NSNotFound) {
            [signal returnNO];
            return;
        }
        
        if ([self.web.loadingURL rangeOfString:@".cdn.baidupcs.com"].location!=NSNotFound) {
            [[self webApiWithCommand:self.web.loadingURL name:@"download" timeout:120] send];
        }
    }else if ([signal is:[HMUIWebView DID_START]]){
        
        NSArray * cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies];
        for (NSHTTPCookie *cookie in cookies) {
            INFO(cookie.domain,cookie.path,cookie.value);
        }
    }
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

ON_Button(signal){
    UIButten *btn = signal.source;
    if ([signal is:[UIButten TOUCH_UP_INSIDE]]) {
        if ([btn is:@"leftBtn"]) {//customNavLeftBtn
            [self backAndRemoveWithAnimate:YES];
            signal.forward = YES;
        }else if ([btn is:@"rightBtn"]){//customNavRightBtn
            
        }
    }
}

ON_WebAPI(dlg){
    if (dlg.sending) {
        
        
    }else if (dlg.succeed){
        //        NSDictionary *dic = [dlg.output objectForKey:[HMWebAPI params]];
        
        
    }else if (dlg.failed){
        if (dlg.timeout) {
            [self showFailureTip:@"操作失败" detail:@"链接超时" timeOut:3.f];
            return;
        }
        
        [self showFailureTip:@"网络链接失败" detail:@"数据不存在或网络错误" timeOut:3.f];
    }else if (dlg.cancelled){
        
    }
    //#if (__ON__ == __HM_DEVELOPMENT__)
    //    CC( [self.class description],dlg.responseData?dlg.responseData:dlg.errorDesc);
    //#endif	// #if (__ON__ == __BEE_DEVELOPMENT__)
}
@end
